/*
 * Copyright 2006-2007 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sant.ant

import org.apache.tools.ant.{Project, Task, BuildException}
import java.io.File
import scala.reflect.BeanProperty

/**
  * @author Stepan Koltsov
  */
class SantTask extends Task {
  @BeanProperty
  var santFile: String = "build.scala"
  
  @BeanProperty
  var dir: File = _
  
  private var targetName: Option[String] = None
  def setTarget(targetName: String) {
    this.targetName = Some(targetName)
  }
  
  private var scalaHome: Option[String] = None
  def setScalaHome(scalaHome: String) {
    this.scalaHome = Some(scalaHome)
  }
  
  private var santHome: Option[String] = None
  def setSantHome(santHome: String) {
    this.santHome = Some(santHome)
  }
  
  private var santJar: Option[String] = None
  def setSantJar(santJar: String) {
    this.santJar = Some(santJar)
  }
  
  var verbose = false
  
  private def scriptFile = new File(dir, santFile)
  
  def santProject = getProject match {
    case null => throw new SantException("task.project is null")
    case x => SantProject.wrap(x)
  }
  
	override def execute() {
    AntVersion.checkVersion()
    
    println("execute")
    if (dir eq null) throw new BuildException("dir is required attribute")
    
    scalaHome.foreach(System.setProperty("scala.home", _))
    santHome.foreach(System.setProperty("sant.home", _))
    santJar.foreach(System.setProperty("sant.jar", _))
    
    println(Env.scalaHome)
    println(Env.santHome)
    println(Env.santJar)
    
    Ant.projectVar.withValue(santProject.createSubProject) {
      try {
        // XXX: parent is not used yet
        
        // XXX: hack        
      	//Ant.project.initWithLogger(Logger.MSG_VERBOSE)
      	
        Ant.project.copyAntBuildListenersFromParent()
        
        log("calling target " + targetName.getOrElse("default") + " in build file " + scriptFile)
        
        Ant.project.load(scriptFile)
        Ant.project.go(targetName.toList)
      } catch {
        case e: SantException =>
          project.log( "BUILD FAILED: " + e.getMessage(), Logger.MSG_ERR )
          throw e
      } finally {
        // XXX: cleanup always
        // currently cannot cleanup because parent project needs files
        if (Ant.projectOption.isEmpty)
          _root_.sant.SantScriptLoader.cleanup
      }
    }
  }
}

object SantTask {
  def defineTask() {
    Ant.taskdef("name" -> "sant", "classname" -> classOf[SantTask].getName)
  }
}